<!--
  @Descripttion:：企业端-职位应聘-简历详情，包含简历、职位、邀约记录、应邀反馈的数据
  @Author：yejianbo
  @Date：2022年05月29日 13:23:29
-->
<template>
  <section>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="面试信息" name="1">
        <el-form
          v-if="inviteForm.inviteDate !== null"
          ref="inviteForm"
          :model="inviteForm"
          label-width="120px"
        >
          <el-form-item label="面试时间" prop="inviteDate">
            <el-date-picker
              v-model="inviteForm.inviteDate"
              type="datetime"
              disabled
              placeholder="选择面试时间"
              format="yyyy-MM-dd HH:mm"
              value-format="yyyy-MM-dd HH:mm"
            />
          </el-form-item>
          <el-form-item label="面试方式" prop="inviteWay">
            <el-checkbox-group
              v-model="inviteForm.inviteWay"
              style="width: 50%"
              disabled
            >
              <el-checkbox
                v-for="item in inviteWay"
                :key="item.value"
                :label="item.value"
              >{{ item.label }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="面试地点" prop="inviteAddress">
            <el-select
              v-model="inviteForm.inviteAddress"
              style="width: 50%"
              disabled
              clearable
            >
              <el-option
                v-for="(item, index) in companyAddress"
                :key="index"
                :value="item.label"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="其他" prop="inviteContent">
            <el-input
              v-model="inviteForm.inviteContent"
              clearable
              disabled
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8 }"
              placeholder="请填写内容"
            />
          </el-form-item>
          <el-form-item label="注意事项" prop="noticeItem">
            <el-checkbox-group
              v-model="inviteForm.noticeItem"
              style="width: 50%"
              disabled
            >
              <el-checkbox
                v-for="item in attentionEvent"
                :key="item.value"
                :label="item.value"
              >{{ item.label }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="发送短信通知" prop="isSendMsg">
            <el-checkbox-group
              v-model="inviteForm.isSendMsg"
              disabled
              style="width: 50%"
            >
              <el-checkbox :label="true">是</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <el-empty v-else description="暂无数据" />
      </el-tab-pane>
      <el-tab-pane label="职位详情" name="2">
        <JobDetail v-if="jobId" ref="jobDetail" />
        <el-empty v-else description="暂无数据" />
      </el-tab-pane>
      <el-tab-pane label="应聘简历" name="3">
        <ResumeReview v-if="resumeId" ref="ResumeReview" />
        <el-empty v-else description="暂无数据" />
      </el-tab-pane>
    </el-tabs>
  </section>
</template>

<script>
import {
  educationOptions,
  studentType,
  workPeriod,
  inviteWay,
  attentionEvent
} from '@/utils/frontDataDict.js'
// 简历详情
import ResumeReview from './components/resumeReview.vue'
// 职位详情
import JobDetail from './components/jobDetail.vue'
import {
  getCopyResume,
  getJobInfo,
  getInvitationRecord,
  getCompanyByLoginUser
} from '@/api/company-admin-api/hireManage/index.js'
import { deepClone } from '../../../utils'
export default {
  // 组件名称
  name: 'InterviewDetail',
  // 局部注册的组件
  components: { ResumeReview, JobDetail },
  // 组件参数 接收来自父组件的数据
  props: {
    jobId: {
      // 职位id
      type: [String, Number]
    },
    resumeId: {
      // 简历id
      type: [String, Number]
    },
    inviteRecoredId: {
      // 邀约记录id
      type: [String, Number]
    }
  },
  // 组件状态值
  data() {
    return {
      companyAddress: [],
      attentionEvent: attentionEvent,
      workPeriod: workPeriod,
      studentType: studentType,
      educationOptions: educationOptions,
      inviteWay: inviteWay,
      activeName: '1',
      hasInviteRecord: false,
      inviteForm: {
        inviteDate: null, // 面试时间
        inviteAddress: '', // 面试地址
        inviteWay: ['0'], // 面试方式（多选），inviteWay 0 笔试，1 面试， 2 线上沟通
        inviteContent: '', // 面试描述内容，其他
        noticeItem: ['0'], // 注意事项 0 请带简历、1 请带学生证、2 请带学历证书、3 请带身份证、4 请带作品、5 请着正装
        isSendMsg: false // isSendMsg: true // todo  '1' 发送 '0' 不发送 缺少是否发送短信的字段
      }
    }
  },
  // 组件方法
  methods: {
    // 获取企业介绍信息接口，拿到企业地址用来下拉选择
    getCompanyRecommentInfo() {
      return getCompanyByLoginUser().then((res) => {
        if (res.code === 0 && res.data) {
          this.companyInfo = res.data
        }
      })
    },
    async init(data) {
      console.log('data>>>', data)
      await this.getCompanyRecommentInfo()
      this.inviteForm = deepClone(data)
      try {
        this.inviteForm.inviteWay = JSON.parse(this.inviteForm.inviteWay)
      } catch (error) {
        this.inviteForm.inviteWay = []
      }
      try {
        this.inviteForm.noticeItem = JSON.parse(this.inviteForm.noticeItem)
      } catch (error) {
        this.inviteForm.noticeItem = []
      }
      this.inviteForm.isSendMsg = !!(this.inviteForm.isSendMsg === '1')
      try {
        console.log('this.companyInfo>>>', this.companyInfo)
        this.companyAddress = JSON.parse(this.companyInfo.companyAddress) || []
        this.companyAddress.map((item, index) => {
          item.label = item.province + item.city + item.address
          item.value = index
          return item
        })
      } catch (error) {
        console.error(new Error('公司地址不存在'))
      }
      this.activeName = '1'
    },
    // tag切换
    handleClick(data) {
      const type = Number(data.index)
      switch (type) {
        case 0:
          break
        case 1:
          this.getJobData()
          break
        case 2:
          this.getResumeData()
          break
      }
    },
    // 获取职位数据
    getJobData() {
      if (this.jobId) {
        const params = {
          id: this.jobId
        }
        getJobInfo(params).then((res) => {
          if (res.code === 0 && res.data) {
            console.log('职位res>>>', res)
            this.$refs.jobDetail.open(res.data)
          }
        })
      }
    },
    // 获取简历数据
    getResumeData() {
      if (this.resumeId) {
        const params = {
          id: this.resumeId
        }
        getCopyResume(params).then((res) => {
          if (res.code === 0 && res.data) {
            console.log('简历res>>>', res)
            setTimeout(() => {
              this.$refs.ResumeReview.open(res.data)
            })
          }
        })
      }
    },
    // 获取邀约记录id
    getInviteRecord() {
      if (this.resumeId) {
        const params = {
          pageNo: 1,
          pageSize: 999,
          sendRecordId: this.inviteRecoredId
        }
        getInvitationRecord(params).then((res) => {
          if (
            res.code === 0 &&
            res.data &&
            Array.isArray(res.data.list) &&
            res.data.list.length &&
            Number(res.data.list[0].status) >= 2
          ) {
            // status 投递记录的简历状态 0 未查看简历、1 已查看简历、2 已发送面试邀约、3 已确认面试邀约、4 已拒绝面试邀约、5 已面试反馈（企业）、6 已评价（学生）
            this.hasInviteRecord = true
            setTimeout(() => {
              this.$refs.InviteRecord.open(res.data.list[0])
            })
          } else {
            this.hasInviteRecord = false
          }
        })
      }
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped></style>
